/*
The MIT License (MIT)

Copyright (c) 2013 Mike Dapiran, Brian May, Richard Pospesel, and Bert Wierenga

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software 
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#pragma once

#include "../Interfaces/ICollisionObject.h"
#include "../Directors/CullingDirector.h"

#include "btBulletCollisionCommon.h"
#include "btBulletDynamicsCommon.h"

namespace Hogshead
{
	namespace BulletPhysics
	{
		class CullingMessage;
		/**
		* A CullingFrustum provides an interface for culling.  This frustrum component requires a sibling CollisionShape
		* which represents the culling geometry.  IMessageReceivers which wish to know about who is in the culling space must
		* register with the CullingFrustum for CullingMessages.
		*/ 
		class CullingFrustum : public ICollisionObject
		{
			CollisionObjectH(Hogshead::BulletPhysics::CullingFrustum, CullingFrustum);
		public:
			CullingFrustum();
			~CullingFrustum();
			void initialize();
			//  updates the frusturms position in the collision world
			void update(const GameTimer&);
		private:
			// activation methods
			void onSetActive();
			void onSetInactive();

			// fills the culling message and forwards it to those who care
			// gets called by the CullingDirector after building the colliding with list and set
			void build_culling_message();

			friend class CullingDirector;
			btCollisionObject* _collision_object;
			btCollisionShape* _collision_shape;

			btTransform* _transform;
			CullingDirector* _culling_director;
			CullingMessage* _culling_message;
			// this list is populated with collision objects
			ArrayList<const btCollisionObject*> _colliding_with_list;
			// a hash set (for fast lookup) of objects we are colliding with
			HashSet<void*> _colliding_with_set;
			// this object's addSingleResult method gets called per contact point
			class FrustrumContactResultCallback : public btCollisionWorld::ContactResultCallback
			{
			public:
				FrustrumContactResultCallback(CullingFrustum* in_frustrum)
				{
					_frustrum =  in_frustrum;
				}
				btScalar addSingleResult (btManifoldPoint &cp, const btCollisionObject *colObj0, int partId0, int index0, const btCollisionObject *colObj1, int partId1, int index1)
				{
					if(_frustrum->_colliding_with_set.add((void*)colObj0))
						_frustrum->_colliding_with_list.add(colObj0);
					return 0.0f;
				}
			private:
				CullingFrustum* _frustrum;
			};
			// passed into contact test in CullingFrustum
			FrustrumContactResultCallback* _callback;
		};
	}
}